﻿using System.Collections.Generic;
using Deferred.Library.Scene;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deferred.Library.Deferred
{
    class AmbientLightRenderEngine
    {
        private DeferredBase deferredBase;

        private Effect ambientLightEffect;
        private IQuadRenderer quadRenderer;

        private EffectParameter lightColorParameter;
        private EffectParameter lightIntensityParameter;

        public AmbientLightRenderEngine(DeferredBase deferredBase)
        {
            this.deferredBase = deferredBase;
        }

        public void Initialize()
        {
            ambientLightEffect = deferredBase.Content.InternalContent.Load<Effect>("AmbientLight");

            lightColorParameter = ambientLightEffect.Parameters["lightColor"];
            lightIntensityParameter = ambientLightEffect.Parameters["lightIntensity"];

            quadRenderer = (IQuadRenderer)deferredBase.Services.GetService(typeof(IQuadRenderer));
        }

        public void drawAmbientLights(IList<AmbientLight> lights)
        {
            foreach (AmbientLight light in lights)
            {
                drawAmbientLight(light);
            }
        }

        private void drawAmbientLight(AmbientLight light)
        {
            lightColorParameter.SetValue(light.Color.ToVector3());
            lightIntensityParameter.SetValue(light.Intensity);

            ambientLightEffect.Begin();
            foreach (EffectPass pass in ambientLightEffect.Techniques["AmbientLight"].Passes)
            {
                pass.Begin();
                quadRenderer.Render(Vector2.One * -1.0f, Vector2.One);
                pass.End();
            }
            ambientLightEffect.End();
        }
    }
}
